<?php
	class Etudiant extends Table {
		public $noEtudiant;
		public $noINE;
		
		public $numSecu;
		
		public $nom;
		public $nomJeuneFille;
		
		public $prenoms; // Tableau contenant les trois prénoms
		
		public $dateNaissance;
		public $villeNaissance;
		
		public $sexe; // Caractère 'M' ou 'F'
		
		public $handicap;
		
		public $situationFamiliale;
		
		public $situationMilitaire;
		
		public $numRueParents;
		public $nomRueParents;
		public $codePostalParents;
		public $villeParents;
		public $paysParents;
		
		public $adresseMailParents;
		public $numTelParents;
		public $numPortParents;
		
		public $typeHebergement;
		
		public $numRueEtudiant;
		public $nomRueEtudiant;
		public $codePostalEtudiant;
		public $villeEtudiant;
		public $paysEtudiant;
		
		public $adresseMailEtudiant;
		public $numTelEtudiant;
		public $numPortEtudiant;
		
		public $catSocioPro_pere;
		public $catSocioPro_mere;
		
		public $hashPassword;
		
		public function __construct($noINE, $numSecu, $nom = '', $nomJeuneFille = '', $prenoms = null, $dateNaissance = 0, $villeNaissance = '', $sexe = '',
									$handicap = null, $situationFamiliale = null, $situationMilitaire = null,
									$numRueParents = '', $nomRueParents = '', $codePostalParents = '', $villeParents = '', $paysParents = '',
									$adresseMailParents = '', $telephoneParents = '', $portableParents = '',
									$typeHebergement = null,
									$numRueEtudiant = '', $nomRueEtudiant = '', $codePostalEtudiant = '', $villeEtudiant = '', $paysEtudiant = '',
									$adresseMailEtudiant = '', $telephoneEtudiant = '', $portableEtudiant = '',
									$catSocioPro_pere = null, $catSocioPro_mere = null, $hashPassword = '') {
			// Initialisation db
			parent::__construct();

			$this->noEtudiant			= -1;
			$this->noINE				= $noINE;
			$this->numSecu				= $numSecu;
			
			$this->nom					= $nom;
			$this->nomJeuneFille		= $nomJeuneFille;
			
			$this->prenoms				= ( is_array($prenoms) ? $prenoms : (!empty($prenoms) ? array($prenoms, '', '') : array('', '', '')));
			
			$this->dateNaissance		= $dateNaissance;
			$this->villeNaissance		= $villeNaissance;
			
			$this->sexe 				= $sexe;
			
			// Objet de type Handicap
			$this->handicap 			= new Handicap('');
			$this->handicap->identifiant = $handicap;
			
			// Objet de type SituationFamiliale
			$this->situationFamiliale 	= new SituationFamiliale('');
			$this->situationFamiliale->identifiant = $situationFamiliale;
			
			// Objet de type SituationMilitaire
			$this->situationMilitaire 	= new SituationMilitaire('');
			$this->situationMilitaire->identifiant = $situationMilitaire;
			
			$this->numRueParents 		= $numRueParents;
			$this->nomRueParents 		= $nomRueParents;
			$this->codePostalParents 	= $codePostalParents;
			$this->villeParents 		= $villeParents;
			$this->paysParents 			= $paysParents;
			
			$this->adresseMailParents 	= $adresseMailParents;
			$this->numTelParents 		= $telephoneParents;
			$this->numPortParents 		= $portableParents;
			
			// Objet de type TypeHebergement
			$this->typeHebergement 		= new TypeHebergement('');
			$this->typeHebergement->identifiant = $typeHebergement;
			
			$this->numRueEtudiant 		= $numRueEtudiant;
			$this->nomRueEtudiant 		= $nomRueEtudiant;
			$this->codePostalEtudiant 	= $codePostalEtudiant;
			$this->villeEtudiant 		= $villeEtudiant;
			$this->paysEtudiant 		= $paysEtudiant;
			
			$this->adresseMailEtudiant 	= $adresseMailEtudiant;
			$this->numTelEtudiant 		= $telephoneEtudiant;
			$this->numPortEtudiant 		= $portableEtudiant;
			
			// Objet de type CatSocioPro
			$this->catSocioPro_pere 	= new CatSocioPro('');
			$this->catSocioPro_pere->identifiant = $catSocioPro_pere;
			
			// Objet de type CatSocioPro
			$this->catSocioPro_mere 	= new CatSocioPro('');
			$this->catSocioPro_mere->identifiant = $catSocioPro_mere;
			
			$this->hashPassword 		= $hashPassword;
		}
		
		public function ajouter() {
			/* INSERT INTO Etudiant VALUES(NULL, '2004010710L', '193039505221327', 
							'CARACOTTE', NULL, 'Jordan', 'Albert', 'Zéphir',
							'1993-03-01', '', 'M', 
							NULL, 5, 1, 
							14, 'rue de Montreuil', '60650', 'Blacourt', 'France', '0344461721', NULL, NULL,
							5, 
							1, 'rue de la Garenne', '80330', 'Cagny', 'France', NULL, '0633274043', 'jordan.caracotte@gmail.com',
							'63', '56'); */
			$st = $this->db->prepare("INSERT INTO Etudiant VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
			
			$valeurs = array($this->noINE,
								$this->numSecu,
								$this->nom,
								$this->nomJeuneFille,
								$this->prenoms[0],
								$this->prenoms[1],
								$this->prenoms[2],
								$this->dateNaissance,
								$this->villeNaissance,
								$this->sexe,
								$this->handicap->identifiant,
								$this->situationMilitaire->identifiant,
								$this->situationFamiliale->identifiant,
								$this->numRueParents,
								$this->nomRueParents,
								$this->codePostalParents,
								$this->villeParents,
								$this->paysParents,
								$this->numTelParents,
								$this->numPortParents,
								$this->adresseMailParents,
								$this->typeHebergement->identifiant,
								$this->numRueEtudiant,
								$this->nomRueEtudiant,
								$this->codePostalEtudiant,
								$this->villeEtudiant,
								$this->paysEtudiant,
								$this->numTelEtudiant,
								$this->numPortEtudiant,
								$this->adresseMailEtudiant,
								$this->catSocioPro_pere->identifiant,
								$this->catSocioPro_mere->identifiant,
								$this->hashPassword);
			
			$st->execute($valeurs);
			$this->noEtudiant = $this->db->lastInsertId();
		}
		
		public function modifier() {
			$st = $this->db->prepare("UPDATE Etudiant SET noINE=?, numSecu=?, nom=?, nomJeuneFille=?, prenom1=?, prenom2=?, prenom3=?, dateNaissance=?, villeNaissance=?, sexe=?, noHandicap=?, noSituationMilitaire=?, noSituationFamiliale=?, numRueParents=?, nomRueParents=?, codePostalParents=?, villeParents=?, paysParents=?, numTelParents=?, numPortParents=?, adresseMailParents=?, noTypeHebergement=?, numRueEtudiant=?, nomRueEtudiant=?, codePostalEtudiant=?, villeEtudiant=?, paysEtudiant=?, numTelEtudiant=?, numPortEtudiant=?, adresseMailEtudiant=?, noCatSocioProPere=?, noCatSocioProMere=?, hashPassword=? WHERE noEtudiant=?");
			
			$valeurs = array($this->noINE,
								$this->numSecu,
								$this->nom,
								$this->nomJeuneFille,
								$this->prenoms[0],
								$this->prenoms[1],
								$this->prenoms[2],
								$this->dateNaissance,
								$this->villeNaissance,
								$this->sexe,
								$this->handicap->identifiant,
								$this->situationMilitaire->identifiant,
								$this->situationFamiliale->identifiant,
								$this->numRueParents,
								$this->nomRueParents,
								$this->codePostalParents,
								$this->villeParents,
								$this->paysParents,
								$this->numTelParents,
								$this->numPortParents,
								$this->adresseMailParents,
								$this->typeHebergement->identifiant,
								$this->numRueEtudiant,
								$this->nomRueEtudiant,
								$this->codePostalEtudiant,
								$this->villeEtudiant,
								$this->paysEtudiant,
								$this->numTelEtudiant,
								$this->numPortEtudiant,
								$this->adresseMailEtudiant,
								$this->catSocioPro_pere->identifiant,
								$this->catSocioPro_mere->identifiant,
								$this->hashPassword,
								$this->noEtudiant);
			$st->execute($valeurs);
		}
		
		public function enregistrer() {	
			if ($this->noEtudiant == -1) {
				$this->ajouter();
			}
			else {
				$this->modifier();
			}
		}
		
		public function completer() {
			if (is_numeric($this->noEtudiant) && $this->noEtudiant > 0) {
				$sql = "SELECT * FROM Etudiant WHERE noEtudiant=?";
				
				$st = $this->db->prepare($sql);
				$st->execute(array($this->noEtudiant));
				
				$this->remplirAvecPDOResult($st->fetch(PDO::FETCH_ASSOC));
			}
			else {
				throw new EtudiantInvalideException();
			}
		}
		
		public function remplirAvecPDOResult($result) {
			if (!is_array($result)) {
				throw new EtudiantInconnuException();
			}
		
			// Chaque champ de la base de données à une équivalence dans l'objet Etudiant
			foreach ($result as $a => $b) {
				/*
					Pour éviter un problème, on vérifie au préalable que la variable existe bien dans l'objet (correspondance avec la base de données complète).
				*/
					
				if (isset($this->$a)) {
					$this->$a = $b;
				}
			}
			
			// Exceptions pour les prénoms et les quelques clés étrangères
			for ($i = 1; $i <= 3; $i++) {
				if (!empty($result['prenom'.$i])) {
					$this->prenoms[$i-1] = $result['prenom'.$i];
				}
			}
			
			$this->handicap 						= new Handicap('');
			$this->handicap->identifiant 			= $result['noHandicap'];

			$this->situationFamiliale 				= new SituationFamiliale('');
			$this->situationFamiliale->identifiant 	= $result['noSituationFamiliale'];

			$this->situationMilitaire 				= new SituationMilitaire('');
			$this->situationMilitaire->identifiant 	= $result['noSituationMilitaire'];

			$this->typeHebergement 					= new TypeHebergement('');
			$this->typeHebergement->identifiant 	= $result['noTypeHebergement'];

			$this->catSocioPro_pere 				= new CatSocioPro('');
			$this->catSocioPro_pere->identifiant 	= $result['noCatSocioProPere'];
			
			$this->catSocioPro_mere 				= new CatSocioPro('');
			$this->catSocioPro_mere->identifiant 	= $result['noCatSocioProMere'];
		}
		
		// ----
		
		static public function recupererAvecNoEtudiant($noEtudiant) {
			$etudiant = new Etudiant('', '');
			$etudiant->noEtudiant = $noEtudiant;
			$etudiant->completer();
			
			return $etudiant;
		}
		
		static public function recupererAvecNoINE($noINE) {
			$etudiant = new Etudiant($noINE, '');
			$etudiant->noINE = $noINE;
			
			$sql = "SELECT * FROM Etudiant WHERE noINE=?";
			$st = DB::get_instance()->prepare($sql);
			$st->execute(array($noINE));
			
			$etudiant->remplirAvecPDOResult($st->fetch(PDO::FETCH_ASSOC));
			
			return $etudiant;
		}
		
		static public function recupererAvecNumSecu($numSecu) {
			$etudiant = new Etudiant('', '');
			$etudiant->numSecu = $numSecu;
			
			$sql = "SELECT * FROM Etudiant WHERE numSecu=?";
			$st = DB::get_instance()->prepare($sql);
			$st->execute(array($numSecu));
			
			$etudiant->remplirAvecPDOResult($st->fetch(PDO::FETCH_ASSOC));
			
			return $etudiant;
		}
		
		static public function recupererAvecAdresseMail($adresseMail) {
			$etudiant = new Etudiant('', '');
			
			$sql = "SELECT * FROM Etudiant WHERE adresseMailEtudiant=?";
			$st = DB::get_instance()->prepare($sql);
			$st->execute(array($adresseMail));
			
			$etudiant->remplirAvecPDOResult($st->fetch(PDO::FETCH_ASSOC));
			
			return $etudiant;
		}
		
		static public function recupererListe() {
			$liste = array();
			
			$sql = "SELECT * FROM Etudiant";
			$st = DB::get_instance()->prepare($sql);
			$st->execute();
			
			foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $element) {
				$etudiant = new Etudiant('', '');
				$etudiant->remplirAvecPDOResult($element);
				
				$liste[] = $etudiant;
			}
			
			return $liste;
		}
	}
	
	// ------------------------------
	
	/*
		Exceptions
	*/
	
	class EtudiantException extends MyException {
		public function __construct() {
			parent::__construct();
		}
	}
	
	class EtudiantInvalideException extends EtudiantException {
		public function __construct() {
			parent::__construct();
		}
	}
	
	class EtudiantInconnuException extends EtudiantException {
		public function __construct() {
			parent::__construct();
		}
	}
?>